import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message, notification } from 'antd';
import { addOrEditValidation, fetchValidation } from '@/utils';
import {
    queryChargeWhiteList,
    opertionChangeStatus,
    queryChargeWhiteDetail,
    opertionChargeWhiteInfo,
    opertionChargeBatchWhiteInfo,
} from './service';


export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetch: Effect;
        status: Effect;
        detail: Effect;
        addOrEdit: Effect;
        batchEdit: Effect;
    };
    reducers: {
        save: Reducer;
        clearDetail: Reducer;
    };
}

const Model: ModelType = {
    namespace: 'logisCenterChargeList',

    state: {
        data: {
            list: [],
            count: 0,
        },
        detailInfo: {},
    },

    effects: {
        * fetch({ payload }, { call, put }) {
            const response = yield call(queryChargeWhiteList, payload);
            const data = fetchValidation(response);
            yield data && put({
                type: 'save',
                payload: { data: data.body },
            });
        },

        * status({ payload: { search, ...query } }, { call, put }) {
            const response = yield call(opertionChangeStatus, query);
            const data = fetchValidation(response);
            if (data) {
                message.success('操作成功');
                yield put({
                    type: 'fetch',
                    payload: search,
                });
            }
        },

        * detail({ payload }, { call, put }) {
            const response = yield call(queryChargeWhiteDetail, payload);
            const data = fetchValidation(response);
            yield data && put({
                type: 'save',
                payload: { detailInfo: data.body },
            });
        },

        * addOrEdit({ payload: { search, ...query } }, { call, put }) {
            const response = yield call(opertionChargeWhiteInfo, query);
            const data = addOrEditValidation(response, query);
            if (data) {
                notification.success({
                    message: query.id ? '编辑成功' : '添加成功'
                });
                yield put({
                    type: 'fetch',
                    payload: search,
                });
            }
        },

        * batchEdit({ payload: { search, ...query } }, { call, put }) {
            const response = yield call(opertionChargeBatchWhiteInfo, query);
            const data = fetchValidation(response);
            if (data) {
                message.success('编辑成功');
                yield put({
                    type: 'fetch',
                    payload: search,
                });
            }
        },
    },

    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },

        clearDetail(state) {
            return {
                ...state,
                detailInfo: {},
            };
        },
    },
};

export default Model;
